package com.planet.stockanalysis.server.main;

import java.net.URL;
import java.text.SimpleDateFormat;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.google.gson.Gson;
import com.planet.stockanalysis.server.entity.Stock;
import com.planet.stockanalysis.server.service.StockService;
import com.planet.stockanalysis.server.socket.WebSocketServer;
import com.planet.stockanalysis.utils.SingleApplicationContext;

public class Main{

	static final String[] FIELDS = new String[]{
		"股票代号","股票名称","行情时间","最新价（元）",
		"昨收盘（元）","今开盘（元）","涨跌额（元）","最低（元）",
		"最高（元）","涨跌幅（%）","成交量（手）","成交额（万元）",
		"竞买价（元）","竞卖价（元）","委比（%）",
		"买一","买二","买三","买四","买五",
		"卖一","卖二","卖三","卖四","卖五",
	};

	public void grab(){
		Stock stock = new Stock();
		try {
			URL url = new URL("http://www.webxml.com.cn/WebServices/ChinaStockWebService.asmx/getStockInfoByCode?theStockCode=sh000001");
			Document d = Jsoup.parse(url,6000);
			Elements elements = d.getElementsByTag("string");
			for(int i=0; i<elements.size(); i++){
				System.out.println(FIELDS[i]+":"+elements.get(i).text());
			}
			int idx = -1;
			//stock.setCode(elements.get(++idx).text());
			stock.setName(elements.get(++idx).text());
			stock.setDate(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(elements.get(++idx).text()));
			stock.setNewPrice(Double.parseDouble(elements.get(++idx).text()));
			System.out.println("stock.getNewPrice()="+stock.getNewPrice());
			stock.setClosingPrice(Double.parseDouble(elements.get(++idx).text()));
			stock.setOpeningPrice(Double.parseDouble(elements.get(++idx).text()));
			stock.setChangeAmount(Double.parseDouble(elements.get(++idx).text()));
			stock.setBottomPrice(Double.parseDouble(elements.get(++idx).text()));
			stock.setTopPrice(Double.parseDouble(elements.get(++idx).text()));
			stock.setChangeRange(Double.parseDouble(elements.get(++idx).text().replaceAll("%", "")));
			stock.setVolume(Double.parseDouble(elements.get(++idx).text()));
			stock.setTurnOver(Integer.parseInt(elements.get(++idx).text()));
			String bidPrice = elements.get(++idx).text();
			if(bidPrice.length()>0)
				stock.setBidPrice(Double.parseDouble(bidPrice));
			String askPrice = elements.get(++idx).text();
			if(askPrice.length()>0)
				stock.setAskPrice(Double.parseDouble(askPrice));
			String commissionRadio = elements.get(++idx).text();
			if(commissionRadio.length()>0)
				stock.setCommissionRadio(Double.parseDouble(commissionRadio));
		} catch (Exception e) {
			e.printStackTrace();
		}

		ClassPathXmlApplicationContext ctx = 
				new ClassPathXmlApplicationContext("spring-cfg.xml"); 
		StockService stockService = (StockService)ctx.getBean("stockService");
		stockService.deleteAll();
		stockService.addStock(stock);
		Gson gson = new Gson();
		System.out.println(gson.toJson(stock));
	}

	public static void main(String[] args) throws Exception {
		SingleApplicationContext.getInstance();
		int port;
		if (args.length > 0) {
			port = Integer.parseInt(args[0]);
		} else {
			port = 8080;
		}
		new WebSocketServer(port).run();
	}
}
